package com.shengzai

import scala.collection.mutable
import scala.io.{BufferedSource, Source}

object Code23CountWorldScala {
  def main(args: Array[String]): Unit = {


    val list: List[String] = Source.fromFile("hadoop_code/src/data/worlds.txt").getLines().toList
    // 切分单词,返回的是地址
    val splitWorlds: List[Array[String]] = list.map((lines: String) => {
      lines.split(" ")
    })

    // 扁平化处理获取所有单词
    val allWorlds: List[String] = splitWorlds.flatMap((words: Array[String]) => {
      words
    })
    //按照相同单词分组,以Map形式保存
    val groupByWorlds: Map[String, List[String]] = allWorlds.groupBy((em: String) => {
      em
    })
    //遍历Map,获取List的长度作为单词数量
    for (elem <- groupByWorlds) {
      val count: Int = elem._2.length
      println(elem._1+"\t"+count)
    }







  }

}
